using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour
{
    public static GameManager Instance { get; private set; }
    void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
        }
        else
        {
            Destroy(gameObject);
        }
    }

    //判断是否无法移动（前排没有位置）
    public bool GetFrontHasPos()
    {
        bool status = true;
        List<Container> containerList = Data.levelsTransform.GetComponent<Levels>().containers;
        Tools.Log("判断游戏状态", containerList.Count.ToString());

        for (int i = 0; i < containerList.Count; i++)
        {
            Container container = containerList[i];
            Goods[] goodsList = container.frontObject.GetComponentsInChildren<Goods>();
            if (goodsList.Length < 3)
            {
                status = false;
                break;
            }
            else
            {
                Debug.Log("没有找到符合类型的组件");
            }
        }
        return status;
    }

    //判断是否存在不可消除（）
    public (bool isGameLose, bool isGameShuffle) GetGameStatus()
    {
        //前排Goods_C 没有goods的数量,如果为0则表示全部满了
        int Goods_C_ChidrenCount = 0;
        Dictionary<int, int> typeDic = new Dictionary<int, int>();

        List<Transform> goodsCTransformList = new List<Transform>();

        List<Container> containerList = Data.levelsTransform.GetComponent<Levels>().containers;

        List<Container> frontUnlockContainers = containerList.FindAll(container =>
            !container.GetComponent<Container>().isLock
            );

        for (int i = 0; i < frontUnlockContainers.Count; i++)
        {
            Container unlockContainer = frontUnlockContainers[i];
            foreach (Transform child in unlockContainer.frontObject.transform)
            {
                goodsCTransformList.Add(child);
            }
        }

        foreach (Transform goodsCT in goodsCTransformList)
        {
            Transform goodsT = goodsCT.Find("Goods");
            if (goodsT == null)
            {
                Goods_C_ChidrenCount++;
            }
            else
            {
                Goods goodsComponent = goodsT.GetComponent<Goods>();
                int key = goodsComponent.goodsId;

                if (typeDic.ContainsKey(key))
                {
                    typeDic[key]++;
                }
                else
                {
                    typeDic[key] = 1;
                }
            }
        }

        // 找到商品出现次数的最大值
        int? max = null;
        foreach (var entry in typeDic)
        {
            if (max == null || entry.Value > max)
            {
                max = entry.Value;
            }
        }

        bool isGameLose = Goods_C_ChidrenCount == 0;  //没位置了
        bool isGameShuffle = (max < 3 && Goods_C_ChidrenCount <= 2); //没有可消除的

        return (isGameLose, isGameShuffle);
    }

    //游戏复活，移除front3个物品
    public void GameRevive()
    {
        List<Goods> frontList = new List<Goods>();
        List<Container> containerList = Data.levelsTransform.GetComponent<Levels>().containers;
        Tools.Log("游戏复活", containerList.Count.ToString());
        for (int i = 0; i < containerList.Count; i++)
        {
            Container container = containerList[i];
            Goods[] goodsList = container.frontObject.GetComponentsInChildren<Goods>();
            if (goodsList.Length > 0)
            {
                for (int j = 0; j < goodsList.Length; j++)
                {
                    frontList.Add(goodsList[j]);
                }
            }
        }

        //随机从frontList中移除3个物品
        for (int i = 0; i < 3; i++)
        {
            int randomIndex = Random.Range(0, frontList.Count);
            Goods goods = frontList[randomIndex];
            frontList.RemoveAt(randomIndex);
            goods.OnFadeOut();
        }
    }
}
